﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows;
using System.Windows.Controls;
using VMuktiAPI;
using System.Text;
/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about 
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
namespace VMukti.Presentation.Controls
{
    public partial class CtlBuddies : System.Windows.Controls.UserControl, IDisposable
    {

        public delegate void DelCloseBuddies();
        public event DelCloseBuddies EntCloseBuddies;
        bool isUserLogin = false;
        StringBuilder sb1 = new StringBuilder();
        delegate void DelAddNewBuddy();
        DelAddNewBuddy delAddNewBuddy = null;
        
        public CtlBuddies()
        {
            try
            {
                this.InitializeComponent();

                VMuktiAPI.VMuktiHelper.RegisterEvent("SucessfulLogin").VMuktiEvent += new VMuktiAPI.VMuktiEvents.VMuktiEventHandler(CtlBuddyList_VMuktiEvent_SucessfulLogin);
                VMuktiAPI.VMuktiHelper.RegisterEvent("LogoutBuddyList").VMuktiEvent += new VMuktiAPI.VMuktiEvents.VMuktiEventHandler(CtlBuddyList_VMuktiEvent_LogoutBuddyList);
                VMuktiAPI.VMuktiHelper.RegisterEvent("RemoveBuddy").VMuktiEvent += new VMuktiEvents.VMuktiEventHandler(CtlBuddies_VMuktiEvent_RemoveBuddy);
                this.Unloaded += new RoutedEventHandler(CtlBuddyList_Unloaded);
                Application.Current.Exit += new ExitEventHandler(Current_Exit);
                this.btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
                txtAddBuddies.KeyDown += new System.Windows.Input.KeyEventHandler(txtAddBuddies_KeyDown);
                delAddNewBuddy = new DelAddNewBuddy(AddNewBuddy);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlBuddies()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void CtlBuddies_VMuktiEvent_RemoveBuddy(object sender, VMuktiEventArgs e)
        {
            try
            {
                //objExpConBuddies.objExpanderItem.Caption
                if (sender != null)
                {
                    //((CtlExpanderItem)((TreeViewItem)sender).Header).Caption 
                    MessageBoxResult msgResult = MessageBox.Show("Do you want to remove " + sender.ToString() + " from your buddylist", "Remove Buddy", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (msgResult == MessageBoxResult.Yes)
                    {
                        try
                        {
                            App.chHttpBootStrapService.svcHttpRemoveBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }
                        catch (System.ServiceModel.EndpointNotFoundException ex)
                        {
                            ex.Data.Add("My Key", "CtlBuddies_VMuktiEvent_RemoveBuddy()--:--CtlBuddies.xaml.cs--:--" + ex.Message + " :--:--");
                            ClsException.LogError(ex);
                            ClsException.WriteToErrorLogFile(ex);
                            VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                            App.chHttpBootStrapService.svcHttpRemoveBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }
                        catch (System.ServiceModel.CommunicationException exp)
                        {
                            exp.Data.Add("My Key", "CtlBuddies_VMuktiEvent_RemoveBuddy()--:--CtlBuddies.xaml.cs--:--" + exp.Message + " :--:--");
                            ClsException.LogError(exp);
                            ClsException.WriteToErrorLogFile(exp);
                            VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                            App.chHttpBootStrapService.svcHttpRemoveBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }

                        try
                        {
                            App.chHttpSuperNodeService.RemoveBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }
                        catch (System.ServiceModel.EndpointNotFoundException exp1)
                        {
                            exp1.Data.Add("My Key", "CtlBuddies_VMuktiEvent_RemoveBuddy()--:--CtlBuddies.xaml.cs--:--" + exp1.Message + " :--:--");
                            ClsException.LogError(exp1);
                            ClsException.WriteToErrorLogFile(exp1);
                            VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                            App.chHttpSuperNodeService.RemoveBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }
                        catch (System.ServiceModel.CommunicationException exp2)
                        {
                            exp2.Data.Add("My Key", "CtlBuddies_VMuktiEvent_RemoveBuddy()--:--CtlBuddies.xaml.cs--:--" + exp2.Message + " :--:--");
                            ClsException.LogError(exp2);
                            ClsException.WriteToErrorLogFile(exp2);
                            VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                            App.chHttpSuperNodeService.RemoveBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, sender.ToString());
                        }

                        for (int i = 0; i < objExpConBuddies.trvExpanderContent.Items.Count; i++)
                        {

                            string str = ((CtlExpanderItem)((TreeViewItem)objExpConBuddies.trvExpanderContent.Items[i]).Header).Caption;
                            if (str.Split('-')[0].ToLower() == sender.ToString())
                            {

                                objExpConBuddies.trvExpanderContent.Items.RemoveAt(i);

                                //MessageBox.Show("Buddy Removed successfully");
                                break;
                            }
                        }
                    }
                }
                //MessageBox.Show(((CtlExpanderItem)objExpConBuddies).Caption);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlBuddies_VMuktiEvent_RemoveBuddy()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }

        }

        void txtAddBuddies_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (e.Key == System.Windows.Input.Key.Enter)
                {
                    btnAdd_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "txtAddBuddies_KeyDown()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }


        bool isAlreadyClick = false;
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (isUserLogin)
            {
                if (txtAddBuddies.Text.Trim() == "")
                {
                    MessageBox.Show("Enter Buddy Name");
                    return;
                }
                else if (txtAddBuddies.Text.ToLower() == VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName.ToLower())
                {
                    MessageBox.Show("You can not add yourself in your buddylist");
                    txtAddBuddies.Text = string.Empty;
                    return;
                }
                else if (!isAlreadyClick)
                {
                    isAlreadyClick = true;
                    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(StartThread4Adding));
                    t.IsBackground = false;
                    t.Priority = System.Threading.ThreadPriority.Normal;
                    //t.SetApartmentState(System.Threading.ApartmentState.MTA);
                    t.Start();
                }
            }
            else
            {
                MessageBox.Show("Please login for adding buddies");
            }

            //this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, delAddNewBuddy);
        }

        void StartThread4Adding()
        {
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, delAddNewBuddy);
        }

        void AddNewBuddy()
        {
            try
            {


                isAlreadyClick = true;
                for (int i = 0; i < objExpConBuddies.trvExpanderContent.Items.Count; i++)
                {

                    string str = ((CtlExpanderItem)((TreeViewItem)objExpConBuddies.trvExpanderContent.Items[i]).Header).Caption;
                    if (str.Split('-')[0].ToLower() == txtAddBuddies.Text.Trim().ToLower())
                    {
                        MessageBox.Show("Requested Buddy already added to your buddylist");
                        return;
                    }
                }

                //dt_RefreshBuddyList.Stop();

                // ** Adding New buddy 

                sb1 = CreateTressInfo();
                System.Text.StringBuilder sb6 = new System.Text.StringBuilder();
                sb6.AppendLine("Buddy:");
                sb6.AppendLine("Adding New buddy .");
                sb6.AppendLine("Buddy name :" + txtAddBuddies.Text);
                sb6.AppendLine(sb1.ToString());
                ClsLogging.WriteToTresslog(sb6);
                // Buddy name :  txtAddBuddies.Text
                

                string sRequestedUser = App.chHttpBootStrapService.svcHttpAddBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, txtAddBuddies.Text.Trim());
                if (sRequestedUser != string.Empty)
                {
                    // ** New buddy Added successfully
                   
                    // Buddy name :  txtAddBuddies.Text
                    // Buddy Status = strStatus
                    string strStatus = sRequestedUser.Substring(sRequestedUser.LastIndexOf('-') + 1);
                    sb1 = CreateTressInfo();
                    System.Text.StringBuilder sb7 = new System.Text.StringBuilder();
                    sb7.AppendLine("Buddy:");
                    sb7.AppendLine("New buddy Added successfully .");
                    sb7.AppendLine("Buddy name :" + txtAddBuddies.Text);
                    sb7.AppendLine("Buddy Status :" + strStatus);
                    sb7.AppendLine(sb1.ToString());
                    ClsLogging.WriteToTresslog(sb7);
                    objExpConBuddies.AddItem(txtAddBuddies.Text.Trim(), true, ImageType.MaleBuddy, strStatus.ToLower());
                    try
                    {
                        App.chHttpSuperNodeService.AddBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, txtAddBuddies.Text.Trim(), strStatus);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException ex)
                    {
                        ex.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + ex.Message + " :--:--");
                        ClsException.LogError(ex);
                        ClsException.WriteToErrorLogFile(ex);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        App.chHttpSuperNodeService.AddBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, txtAddBuddies.Text.Trim(), strStatus);
                    }
                    catch (System.ServiceModel.CommunicationException exp)
                    {
                        exp.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + exp.Message + " :--:--");
                        ClsException.LogError(exp);
                        ClsException.WriteToErrorLogFile(exp);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        App.chHttpSuperNodeService.AddBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, txtAddBuddies.Text.Trim(), strStatus);
                    }

                    txtAddBuddies.Text = string.Empty;
                }
                else
                {
                    MessageBox.Show("Requested Buddy is not register yet");
                }

            }

            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);


            }
            finally
            {
                //  dt_RefreshBuddyList.Start();
                isAlreadyClick = false;
            }

        }

        public void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (EntCloseBuddies != null)
                {
                    EntCloseBuddies();
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnClose_Click()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void LoadBuddyList()
        {
            //objBuddyList.LoadBuddyList();
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            try
            {
                LogOutBuddyList();
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("SucessfulLogin");
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("LogoutBuddyList");
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("RemoveBuddy");

            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Current_Exit()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void CtlBuddyList_Unloaded(object sender, RoutedEventArgs e)
        {
            try
            {
                LogOutBuddyList();
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("SucessfulLogin");
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("LogoutBuddyList");
                VMuktiAPI.VMuktiHelper.UnRegisterEvent("RemoveBuddy");
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlBuddyList_Unloaded()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void LogOutBuddyList()
        {
            try
            {
                if (dt_RefreshBuddyList != null)
                {
                    dt_RefreshBuddyList.Stop();                    
                }
                isUserLogin = false;
                objExpConBuddies.trvExpanderContent.Items.Clear();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LogOutBuddyList()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void CtlBuddyList_VMuktiEvent_LogoutBuddyList(object sender, VMuktiAPI.VMuktiEventArgs e)
        {
            try
            {
                LogOutBuddyList();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlBuddyList_VMuktiEvent_LogoutBuddyList()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void CtlBuddyList_VMuktiEvent_SucessfulLogin(object sender, VMuktiAPI.VMuktiEventArgs e)
        {
            //  fncOpenClient();
            try
            {
                isUserLogin = true;
                fncStartTimer();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlBuddyList_VMuktiEvent_SucessfulLogin()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }

        }

        # region New Functions for Buddylist

        string strFilePath = string.Empty;
        string appPath = string.Empty;
        System.Windows.Threading.DispatcherTimer dt_RefreshBuddyList = null;

        delegate void DelTimerTick();
        DelTimerTick delDelTimerTick = null;

        delegate void DelRefreshBuddyList();
        DelRefreshBuddyList delRefreshBuddyList = null;

        void fncStartTimer()
        {
            try
            {
                // ** Starting BuddyList Refreshing Timer
                sb1 = CreateTressInfo();
                System.Text.StringBuilder sb7 = new System.Text.StringBuilder();
                sb7.AppendLine("Buddy:");
                sb7.AppendLine("Starting BuddyList Refreshing Timer .");
                sb7.AppendLine(sb1.ToString());
                ClsLogging.WriteToTresslog(sb7);

                dt_RefreshBuddyList = new System.Windows.Threading.DispatcherTimer();
                dt_RefreshBuddyList.Interval = TimeSpan.FromSeconds(5);
                dt_RefreshBuddyList.Tick += new EventHandler(dt_RefreshBuddyList_Tick);
                dt_RefreshBuddyList.IsEnabled = true;
               // dt_RefreshBuddyList.Start();
                delDelTimerTick = new DelTimerTick(fncGetBuddyList);
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, delDelTimerTick);
                //fncGetBuddyList();
                appPath = AppDomain.CurrentDomain.BaseDirectory;
                delRefreshBuddyList = new DelRefreshBuddyList(RefreshBuddyList);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "fncStartTimer()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }


        }

        #region Refreshing Buddy Status
        // For SuperNode Buddy RefreshTick.
        List<string> fncGetBuddyStatus(string uName)
        {
            string ClientConnectionString = @"Data Source=" + System.IO.Path.GetDirectoryName(appPath) + "\\SuperNodeBuddyInfo.sdf";
            SqlCeConnection conn = new SqlCeConnection(ClientConnectionString);
            conn.Open();
            List<string> lstOnlineBuddies = new List<string>();
            try
            {
                string str = "SELECT * FROM User_BuddyList Where UserName ='" + uName + "'";
                SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(str, conn);

                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);

                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    lstOnlineBuddies.Add(dataTable.Rows[i]["BuddyName"].ToString() + "-" + dataTable.Rows[i]["BuddyStatus"].ToString());
                }
                dataAdapter.Dispose();
                conn.Close();
                conn.Dispose();
                return lstOnlineBuddies;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "SuperNodeServiceDomain_EntHttpGetBuddies()--:--SuperNodeDomain.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                conn.Close();
                conn.Dispose();
                return lstOnlineBuddies;
            }

        }


        void dt_RefreshBuddyList_Tick(object sender, EventArgs e)
        {
            try
            {
                ((System.Windows.Threading.DispatcherTimer)sender).Stop();
                //System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StartThread));
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(StartThread));
                t.IsBackground = false;
                t.Priority = System.Threading.ThreadPriority.Normal;                
                //t.SetApartmentState(System.Threading.ApartmentState.MTA);
                t.Start();


            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "fncStartTimer()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }


        }

        void StartThread()
        {
            try
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, delRefreshBuddyList);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "fncStartTimer()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }

        }

        void RefreshBuddyList()
        {
            try
            {
                List<string> lstuddyStatus = new List<string>();
                //if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithNetP2P)
                //{
                //    lstuddyStatus = App.chHttpSuperNodeService.GetBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                //}
                //else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.BootStrap)
                //{
                //    // Change is no timer tick here only through its own database
                //    //lstuddyStatus = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                //    lstuddyStatus = fncGetBuddyStatus(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                //}
                //lstCheckBuddyStatus.Clear();
                try
                {
                    lstuddyStatus = App.chHttpSuperNodeService.GetBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                }
                catch (System.ServiceModel.EndpointNotFoundException ex)
                {
                    ex.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + ex.Message + " :--:--");
                    ClsException.LogError(ex);
                    ClsException.WriteToErrorLogFile(ex);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    lstuddyStatus = App.chHttpSuperNodeService.GetBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                }
                catch (System.ServiceModel.CommunicationException exp)
                {
                    exp.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + exp.Message + " :--:--");
                    ClsException.LogError(exp);
                    ClsException.WriteToErrorLogFile(exp);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    lstuddyStatus = App.chHttpSuperNodeService.GetBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                }
                

                for (int i = 0; i < lstuddyStatus.Count; i++)
                {
                    string[] strBuddyStatus = lstuddyStatus[i].Split('-');
                    if (strBuddyStatus[0] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName)
                    {
                        objExpConBuddies.IsItemAdded(strBuddyStatus[0], strBuddyStatus[1].ToLower());
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "dt_RefreshBuddyList_Tick() BootStrap node or supernode is down--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            finally
            {
                dt_RefreshBuddyList.Start();
            }

        }

        #endregion
        // Fill List at first time when login.
        private void fncGetBuddyList()
        {
            try
            {
                objExpConBuddies.trvExpanderContent.Items.Clear();

                List<string> lsBuddies = new List<string>();

                try
                {
                    // ** Taking My Buddy First time
                    sb1 = CreateTressInfo();
                    System.Text.StringBuilder sb7 = new System.Text.StringBuilder();
                    sb7.AppendLine("Buddy:");
                    sb7.AppendLine("Taking My Buddy First time.");
                    sb7.AppendLine(sb1.ToString());
                    ClsLogging.WriteToTresslog(sb7);

                    lsBuddies = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);

                    
                }
                catch (System.ServiceModel.EndpointNotFoundException ex)
                {
                    ex.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + ex.Message + " :--:--");
                    ClsException.LogError(ex);
                    ClsException.WriteToErrorLogFile(ex);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    lsBuddies = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                }
                catch (System.ServiceModel.CommunicationException exp)
                {
                    exp.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + exp.Message + " :--:--");
                    ClsException.LogError(exp);
                    ClsException.WriteToErrorLogFile(exp);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    lsBuddies = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                }

                for (int i = 0; i < lsBuddies.Count; i++)
                {
                    string[] strBuddyStatus = lsBuddies[i].Split('-');
                    if (strBuddyStatus[0] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName) //&& !lstBuddies.Items.Contains(lsBuddies[i].ToString()))
                    {
                        objExpConBuddies.AddItem(strBuddyStatus[0], true, ImageType.MaleBuddy, strBuddyStatus[1].ToLower());
                    }
                }
                try
                {
                    App.chHttpSuperNodeService.AddNodeBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, lsBuddies);
                }
                catch (System.ServiceModel.EndpointNotFoundException ex)
                {
                    ex.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + ex.Message + " :--:--");
                    ClsException.LogError(ex);
                    ClsException.WriteToErrorLogFile(ex);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    App.chHttpSuperNodeService.AddNodeBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, lsBuddies);
                }
                catch (System.ServiceModel.CommunicationException exp)
                {
                    exp.Data.Add("My Key", "AddNewBuddy()--:--CtlBuddies.xaml.cs--:--" + exp.Message + " :--:--");
                    ClsException.LogError(exp);
                    ClsException.WriteToErrorLogFile(exp);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    App.chHttpSuperNodeService.AddNodeBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, lsBuddies);
                }




                //if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.BootStrap)
                //{
                //    List<string> lsBuddies = new List<string>();

                //    lsBuddies = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                //    for (int i = 0; i < lsBuddies.Count; i++)
                //    {
                //        string[] strBuddyStatus = lsBuddies[i].Split('-');
                //        if (strBuddyStatus[0] != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName) //&& !lstBuddies.Items.Contains(lsBuddies[i].ToString()))
                //        {

                //            objExpConBuddies.AddItem(strBuddyStatus[0], true, ImageType.MaleBuddy, strBuddyStatus[1].ToLower());

                //        }
                //    }
                //}

                //else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithNetP2P)
                //{

                //    List<string> lsBuddies = new List<string>();

                //    lsBuddies = App.chHttpSuperNodeService.GetBuddies(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);

                //    if (lsBuddies.Count == 0)
                //    {
                //        lsBuddies = App.chHttpBootStrapService.svcHttpGetSuperNodeBuddyList(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                //    }

                //    for (int i = 0; i < lsBuddies.Count; i++)
                //    {
                //        string[] strBuddyStatus = lsBuddies[i].Split('-');
                //        if (lsBuddies[i].ToString() != VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName) //&& !lstBuddies.Items.Contains(lsBuddies[i].ToString()))
                //        {
                //            objExpConBuddies.AddItem(strBuddyStatus[0], true, ImageType.MaleBuddy, strBuddyStatus[1].ToLower());
                //        }
                //    }
                //}




            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "fncGetBuddyList()--:--CtlBuddies.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (EntCloseBuddies != null)
            {
                EntCloseBuddies = null;
            }
        }

        #endregion

        #region logging function

        public static StringBuilder CreateTressInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("User Is : " + VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
            sb.AppendLine();
            sb.Append("Peer Type is : " + VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType.ToString());
            sb.AppendLine();
            sb.Append("User's SuperNode is : " + VMuktiAPI.VMuktiInfo.CurrentPeer.SuperNodeIP);
            sb.AppendLine();
            sb.Append("User's Machine Ip Address : " + VMuktiAPI.GetIPAddress.ClsGetIP4Address.GetIP4Address());
            sb.AppendLine();
            sb.AppendLine("----------------------------------------------------------------------------------------");
            return sb;
        }

        #endregion

    }



}
